<?php
// +----------------------------------------------------------------------
// | ZanPianCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpiancms.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.zanpiancms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use app\common\library\Insert;
use think\Cache;
use think\Model;
class Cm extends Model{
	 protected $auto = ['cm_addtime','cm_ip','cm_status','cm_content'];
	 public function user(){
		return $this->hasOne('user','user_id','cm_uid');
     }	 
	 protected function setcmaddtimeAttr($value,$data){
		return time();
	 }
	protected function setcmipAttr(){
		return ip2long(request()->ip());
	}
    protected function setcmcontentAttr($value){
		if($GLOBALS['config']['user']['sys']['user_replace']){
		    $array = explode('|',$GLOBALS['config']['user']['sys']['user_replace']);
		    $content= trim(str_replace($array,'*',strip_tags($value)));
		}else{
		    $content= strip_tags($value);
		}
		return $content;
	 }	
	 
	 protected function setcmstatusAttr($value){
		if(!empty($value)){
		    return $value;	
		}else{
		    if($GLOBALS['config']['user']['sys']['user_check']==1){
			    return 0;
		    }else{
			    return 1;
			}
		}
	}	
    public function pidcms(){
        return $this->hasMany('cm','cm_pid');
    }
    //数据查询	
    public function listData($tag){
		$param = zanpiancms_param_url();
		if(!is_array($tag)) {
            $tag=json_decode($tag, true);
        }
		$tag['field']= !empty($tag['field']) ? $tag['field'] : '*';
		$tag['limit']= !empty($tag['limit']) ? $tag['limit'] : 10;
		if(!empty($tag['page']) && !empty($param['order'])){
			$tag['order']='cm_'.$param['order'].' desc';
		}		
		if(empty(order_check($tag['order']))){
			$tag['order']='cm_addtime desc';
		}
		$where=[];
		if(empty($tag['status'])) {
		   $where['cm_status']=['eq',1];
		}
	    if(!empty($tag['vid'])) {
		    $where['cm_vid']=array('eq',$tag['vid']);
	    }
	    if(isset($tag['pid'])) {
		    $where['cm_pid']=array('eq',$tag['pid']);
	    }		
	    if(!empty($tag['uid'])) {
		    $where['cm_uid'] = array('eq',$tag['uid']);
	    }
	    if(!empty($tag['tid'])) {
		    $where['cm_tid'] = array('eq',$tag['tid']);
	    }		
	    if(!empty($tag['sid'])) {
		    $where['cm_sid'] = array('eq',$tag['sid']);
	    }	
        if(!empty($tag['addtime'])){
		    $time = explode(',',$tag['addtime']);
		    if(count($time) > 1) {
			    $where['cm_addtime'] = array('between',intval(strtotime($time[0])).','.intval(strtotime($time[1])));
		    }else{
			    $where['cm_addtime'] = array('gt',intval(strtotime($time[0])));
		    }			
        }	
        if(!empty($tag['page'])){
		    $data = db('cm')->join('user user','user.user_id = cm_uid','LEFT')->where($where)->field($tag['field'])->order($tag['order'])->paginate($tag['limit'],false);
		}else{
			$limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));
			$data = db('cm')->join('user user','user.user_id = cm_uid','LEFT')->where($where)->order($tag['order'])->limit($limit_str)->select();
		}
		return $data;
    } 
}